package com.zxy.ziems.server.device.service.impl;

import cn.hutool.core.util.StrUtil;
import com.zxy.ziems.server.device.convertor.DeviceConvertor;
import com.zxy.ziems.server.device.mapper.DataTypeMapper;
import com.zxy.ziems.server.device.pojo.dto.DataTypeDTO;
import com.zxy.ziems.server.device.pojo.entity.DataTypeEntity;
import com.zxy.ziems.server.device.pojo.vo.DataTypeTreeVO;
import com.zxy.ziems.server.device.pojo.vo.DataTypeVO;
import com.zxy.ziems.server.device.service.DataTypeService;
import com.zxy.ziems.server.mybatis.core.query.WrapperX;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import com.zxy.ziems.server.utils.AssertUtil;
import com.zxy.ziems.server.utils.tree.TreeUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;

/**
 * @author 10348359
 * @create 2024-03-20 14:15
 */
@Service
public class DataTypeServiceImpl extends ServiceImplX<DataTypeMapper, DataTypeEntity> implements DataTypeService {
    @Override
    public DataTypeEntity add(DataTypeDTO dto) {
        AssertUtil.isNull(getByCode(dto.getCode()), "编码重复");
        dto.getGroupNameList().forEach((group) -> {
            AssertUtil.isTrue(StrUtil.isNotBlank(group), "分组名称不能为空");
        });
        DataTypeEntity entity = dto.buildEntity();
        save(entity);
        return entity;
    }

    @Override
    public void update(DataTypeDTO dto) {
        AssertUtil.nonNull(getById(dto.getId()), "数据类型不存在");
        DataTypeEntity old = getByCode(dto.getCode());
        AssertUtil.isTrue(Objects.isNull(old) || old.getId().equals(dto.getId()), "编码重复");
        dto.getGroupNameList().forEach((group) -> {
            AssertUtil.isTrue(StrUtil.isNotBlank(group), "分组名称不能为空");
        });
        DataTypeEntity entity = dto.buildEntity();
        updateById(entity);
    }

    @Override
    public void deleteById(String id) {
        if (StrUtil.isBlank(id)) {
            return;
        }
        removeById(id);
    }

    @Override
    public List<DataTypeTreeVO> tree() {
        List<DataTypeEntity> entityList = list();
        List<DataTypeTreeVO> treeList = new ArrayList<>(entityList.size());
        Map<String, DataTypeTreeVO> treeMap = new HashMap<>();

        List<Function<DataTypeEntity, String>> functionList = Arrays.asList(
                DataTypeEntity::getGroupLevel1Name,
                DataTypeEntity::getGroupLevel2Name,
                DataTypeEntity::getGroupLevel3Name,
                DataTypeEntity::getGroupLevel4Name,
                DataTypeEntity::getGroupLevel5Name);
        entityList.forEach((entity) -> {
            String parentGroupCode = "";
            for (int i = 0; i < functionList.size(); i++) {
                Function<DataTypeEntity, String> f = functionList.get(i);
                String group = f.apply(entity);
                if (StrUtil.isBlank(group)) {
                    break;
                }
                String key = 0 == i ? group : parentGroupCode + "_" + group;
                if (treeMap.containsKey(key)) {
                    parentGroupCode = key;
                    continue;
                }

                DataTypeTreeVO treeEntity = DataTypeTreeVO.builder()
                        .name(group)
                        .groupCode(key)
                        .parentGroupCode(parentGroupCode)
                        .id(key)
                        .code(key)
                        .isGroup(true)
                        .build();
                parentGroupCode = key;
                treeMap.put(key, treeEntity);
                treeList.add(treeEntity);
            }

            DataTypeTreeVO treeEntity = DataTypeTreeVO.builder()
                    .name(entity.getName())
                    .code(entity.getCode())
                    .groupCode(entity.getId())
                    .parentGroupCode(parentGroupCode)
                    .id(entity.getId())
                    .isGroup(false)
                    .build();
            treeList.add(treeEntity);
        });
        return TreeUtils.tree(treeList);
    }

    @Override
    public DataTypeEntity getByCode(String code) {
        return getOne(DataTypeEntity::getCode, code);
    }

    @Override
    public DataTypeEntity getByName(String name) {
        return getOne(WrapperX.<DataTypeEntity>lambdaQuery()
                .eq(DataTypeEntity::getName, name)
        );
    }

    @Override
    public DataTypeEntity getById(String id) {
        return super.getById(id);
    }

    @Override
    public List<DataTypeEntity> listByIdList(Collection<String> idList) {
        return super.listByIds(idList);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<DataTypeVO> listAll() {
        List<DataTypeEntity> dataTypeEntityList = this.list();
        List<DataTypeVO> dataTypeVOList = DeviceConvertor.INSTANCE.dataTypDOListConvert2VOList(dataTypeEntityList);
        return dataTypeVOList;
    }

}
